package edu.iss.demo.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;










import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import edu.iss.demo.exception.EmloyeeNotFound;
import edu.iss.demo.model.Employee;
import edu.iss.demo.model.LeaveType;

import edu.iss.demo.service.EmployeeService;
import edu.iss.demo.service.LeaveTypeService;


@Controller
public class AdminControllerM 
{
	
	@Autowired
	private LeaveTypeService leaveTypeService;
	
	@Autowired
	private EmployeeService empservice;
	
	//Admin Navigation
	
	@RequestMapping(value = "/admin", method = RequestMethod.GET)
	public ModelAndView adminView()
	{
		return new ModelAndView("admin_createStaff");
	}
	
	@RequestMapping(value = "/admin/manageleavetype", method = RequestMethod.GET)
	public ModelAndView vieLeaveType()
	{
		return new ModelAndView("admin_leaveType");
	}
	

	
	@RequestMapping(value = "/admin/leavetype", method = RequestMethod.POST, params="save")
	public ModelAndView createLeaveType(@ModelAttribute("SpringWeb")LeaveType leavetype, ModelMap model,final RedirectAttributes redirectAttributes)
	{
		ModelAndView mav = new ModelAndView();
		leaveTypeService.create(leavetype);	
		mav.setViewName("redirect:/admin/manageleavetype");
		String message = "New Leave Type " + leavetype.getDescription()
				+ " was successfully created.";

		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}
	
	@RequestMapping(value = "/admin/leavetype", method = RequestMethod.POST, params="clear")
	public ModelAndView clearLeaveType(ModelMap models)
	{
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:/admin/manageleavetype");
		return mav;
	}
	@RequestMapping(value ="/admin/view-all",method =RequestMethod.GET)
	public ModelAndView viewallEmp()	
	{
		ModelAndView mav=new ModelAndView("admin_staffListing");
		List<Employee> emplist=empservice.findAll();
		mav.addObject("elist", emplist);
		return mav;				
	}
	
		public String superID;
		@RequestMapping(value ="/admin/edit-details/{id}", method=RequestMethod.GET)
		public ModelAndView edit_Emp(@PathVariable String id, final RedirectAttributes rdr)	
		{
			Employee e=empservice.findById(id);
			ModelAndView mav=new ModelAndView("admin_editStaff");
			mav.addObject("emp",e);
			superID=id;
			return mav;
		}
		
		@RequestMapping(value = "/admin/edit-details/editstaff", method = RequestMethod.POST, params="delete")
		public ModelAndView clearUpdateEmployee(ModelMap models)
		{
			Employee e=empservice.findById(superID);
			ModelAndView mav=new ModelAndView("admin_editStaff");
			mav.addObject("emp",e);
			return mav;
		}

		@RequestMapping(value = "/admin/edit-details/editstaff", method = RequestMethod.POST, params="save")
		public ModelAndView UpdateEmployee(@ModelAttribute("SpringWeb")Employee em,final RedirectAttributes redirectAttributes)
		{
			Employee eold=empservice.findById(superID);
			eold.setName(em.getName());
			eold.setPassword(em.getPassword());
			eold.setDesignation(em.getDesignation());
			eold.setContactNo(em.getContactNo());
			eold.setAddress(em.getAddress());
			eold.setManagerId(em.getManagerId());
			eold.setRole(em.getRole());
			eold.setML_Balance(em.getML_Balance());
			eold.setAL_Balance(em.getAL_Balance());
			
			try 
			{
				ModelAndView mav=new ModelAndView();
				mav.setViewName("redirect:/admin/view-all");
				Employee enew=empservice.update(eold);
				String message = "Staff " + enew.getName()
						+ " was successfully updated.";
				redirectAttributes.addFlashAttribute("msg", message);
				return mav;
				
			} 
			catch (EmloyeeNotFound e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
			
			
		}
	
		

		

	
	
	
	
	
	
	
	

}
